package java67_adv7;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

public class ClsTest2 {

	public static void main(String[] args) throws Exception {
//		Class c = Class.forName("java.util.TreeMap");
//		Map map = (Map) c.newInstance();// 无参的构造方法

		Class c = Class.forName("java67_adv7.Student");
//		Student st = (Student) c.newInstance();
//		System.out.println(st);
		// 找到对应的构造方法:一个String和一个int作为参数的构造方法
		Constructor cr = c.getDeclaredConstructor(String.class, int.class);
		// 通过cr来创建对象
		Student st = (Student) cr.newInstance("徐云", 20);
		// Student st2=new Student("xunyib",21);
		System.out.println(st);

		Constructor c2 = c.getDeclaredConstructor(int.class);
		c2.setAccessible(true);// 取消对权限的限制
		Student s2 = (Student) c2.newInstance(31);

		System.out.println(s2);
		Field fname = c.getDeclaredField("name");
		fname.setAccessible(true);
		fname.set(s2, "川皇");/// !! obj.name=xxx
		System.out.println("改了name属性后:" + s2);
		Method mage = c.getDeclaredMethod("changeAge", int.class);
		mage.setAccessible(true);
		Object o = mage.invoke(s2, 71);
		System.out.println("方法返回了:" + o);
		System.out.println("调用方法后:" + s2);
	}

}
